package Casino;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

import rmi.Hello;

public class CasinoVirtual extends JFrame implements ICasino {

	private JPanel contentPane;
	static Registry registry;
	static CasinoVirtual frame;
	static ICasino stub;
    static IJuego stub2;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new CasinoVirtual();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	
		//SERVIDOR
		
 		try {
			registry = LocateRegistry.createRegistry(2500);  
		    
		    stub = (ICasino) UnicastRemoteObject.exportObject(frame, 0);
		    registry.rebind("Casino", stub);
		    
		    System.err.println("Casino listo");
		} catch (Exception e) {
		    System.err.println("Server exception: " + e.toString());
		    e.printStackTrace();
		}
 		
 		// SERVIDOR
 		
 		
		
	}

	/**
	 * Create the frame.
	 */
	public CasinoVirtual() {
		setTitle("Mini Casino Virtual");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 263, 219);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnCraps = new JButton("Craps");
		btnCraps.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				    registry = LocateRegistry.getRegistry(2500);
				    stub2 = (IJuego) registry.lookup("Crasp");
				    stub2.ponerSaldo(100);
				    
				} catch (Exception e1) {
				    System.err.println("Client exception: " + e1.toString());
				    e1.printStackTrace();
				}
				
				
			}
		});
		btnCraps.setBounds(60, 45, 123, 23);
		contentPane.add(btnCraps);
		
		JButton btnEscoba = new JButton("Escoba");
		btnEscoba.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				    registry = LocateRegistry.getRegistry(2500);
				    stub2 = (IJuego) registry.lookup("Escoba");
				    stub2.ponerSaldo(100);
				    
				} catch (Exception e1) {
				    System.err.println("Client exception: " + e1.toString());
				    e1.printStackTrace();
				}
			}
		});
		btnEscoba.setBounds(60, 79, 123, 23);
		contentPane.add(btnEscoba);
		
		JButton btnGboard = new JButton("GBoard");
		btnGboard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				    registry = LocateRegistry.getRegistry(2500);
				    stub2 = (IJuego) registry.lookup("GBoard");
				    stub2.ponerSaldo(100);
				    
				} catch (Exception e1) {
				    System.err.println("Client exception: " + e1.toString());
				    e1.printStackTrace();
				}
			}
		});
		btnGboard.setBounds(60, 113, 123, 23);
		contentPane.add(btnGboard);
		
		JButton btnMastermind = new JButton("Mastermind");
		btnMastermind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				    registry = LocateRegistry.getRegistry(2500);
				    stub2 = (IJuego) registry.lookup("Mastermind");
				    stub2.ponerSaldo(100);
				    
				} catch (Exception e1) {
				    System.err.println("Client exception: " + e1.toString());
				    e1.printStackTrace();
				}
			}
		});
		btnMastermind.setBounds(60, 147, 123, 23);
		contentPane.add(btnMastermind);
		
		JButton btnParejasSimpsons = new JButton("Parejas Simpsons");
		btnParejasSimpsons.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				    registry = LocateRegistry.getRegistry(2500);
				    stub2 = (IJuego) registry.lookup("Parejas");
				    stub2.ponerSaldo(100);
				    
				} catch (Exception e1) {
				    System.err.println("Client exception: " + e1.toString());
				    e1.printStackTrace();
				}
			}
		});
		btnParejasSimpsons.setBounds(60, 11, 123, 23);
		contentPane.add(btnParejasSimpsons);
	}
	
	 

	@Override
	public void acabado() throws RemoteException {
		
		System.out.println("El juego ha acabado");
		System.out.println(stub2.darSaldo());
		
	}
}
